package com.freshmarket.freshmarket.service.impl;

import com.freshmarket.freshmarket.controller.form.InsertProductBatchForm;
import com.freshmarket.freshmarket.dao.ProductBatchOperationMapper;
import com.freshmarket.freshmarket.dao.ProductMapper;
import com.freshmarket.freshmarket.entity.Product;
import com.freshmarket.freshmarket.entity.ProductBatch;
import com.freshmarket.freshmarket.dao.ProductBatchMapper;
import com.freshmarket.freshmarket.entity.ProductBatchOperation;
import com.freshmarket.freshmarket.exception.ArgsMistakeException;
import com.freshmarket.freshmarket.service.IProductBatchService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.freshmarket.freshmarket.utils.Utils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Squirrel
 * @since 2020-09-05
 */
@Service
public class ProductBatchServiceImpl extends ServiceImpl<ProductBatchMapper, ProductBatch> implements IProductBatchService {
    final ProductMapper productMapper;
    final ProductBatchMapper productBatchMapper;
    final ProductBatchOperationMapper productBatchOperationMapper;

    public ProductBatchServiceImpl(ProductMapper productMapper, ProductBatchMapper productBatchMapper, ProductBatchOperationMapper productBatchOperationMapper) {
        this.productMapper = productMapper;
        this.productBatchMapper = productBatchMapper;
        this.productBatchOperationMapper = productBatchOperationMapper;
    }

    /**
     * @author Squbi
     * @date 2020/9/7 11:17
     * @Description 新增批次
     */
    @Transactional
    @Override
    public void insertProductBatch(InsertProductBatchForm insertProductBatchForm){
        Product product = productMapper.selectById(insertProductBatchForm.getProductId());
        if(product==null){
            throw new ArgsMistakeException("不存在的商品id");
        }

        ProductBatch productBatch = new ProductBatch();
        productBatch.setProductBatchNum(insertProductBatchForm.getProductBatchNum());
        productBatch.setProductId(insertProductBatchForm.getProductId());
        productBatch.setProductBatchSource(insertProductBatchForm.getProductBatchSource());
        productBatchMapper.insert(productBatch);

        ProductBatchOperation productBatchOperation = new ProductBatchOperation();
        productBatchOperation.setProductBatchOperationTime(Utils.getDateTime());
        productBatchOperation.setProductBatchOperationStatus("上架");
        productBatchOperation.setProductBatchId(productBatch.getProductBatchId());
        productBatchOperationMapper.insert(productBatchOperation);

        if(product.getProductNum()==0){
            product.setProductStatus("已上架");
        }
        product.setProductNum(product.getProductNum()+insertProductBatchForm.getProductBatchNum());
        productMapper.updateById(product);
    }

    /**
     * @author Squbi
     * @date 2020/9/7 11:17
     * @Description 下架批次
     */
    @Override
    @Transactional
    public void putDownProductBatch(int productBatchId){
        ProductBatch productBatch = productBatchMapper.selectById(productBatchId);
        if(productBatch.getProductBatchNum()==0){
            throw new ArgsMistakeException("该批次已下架");
        }
        Product product = productMapper.selectById(productBatch.getProductId());
        product.setProductNum(product.getProductNum()-productBatch.getProductBatchNum());
        if(product.getProductNum()<=0){
            product.setProductStatus("已下架");
        }
        productMapper.updateById(product);
        productBatch.setProductBatchNum(0);
        productBatchMapper.updateById(productBatch);
        ProductBatchOperation productBatchOperation=new ProductBatchOperation();
        productBatchOperation.setProductBatchId(productBatchId);
        productBatchOperation.setProductBatchOperationStatus("下架");
        productBatchOperation.setProductBatchOperationTime(Utils.getDateTime());
        productBatchOperationMapper.insert(productBatchOperation);
    }
}
